<?php

/**
 *
 * @author huangjian E-mail:870596179@qq.com
 * @version 创建时间：2016年10月21日 上午10:42:24
 * 类说明
 */
class ShopAction extends BaseAction
{

    /**
     * @authority 浏览
     */
    public function index()
    {
        $this->display('index');
    }

    //列表
    public function getAllData()
    {
        $Model = M($this->getActionName());
        $UserModel = M("User");
        $SchoolAreaModel = M("SchoolAreas");
        $ShopProductModel = M("ShopProduct");
        $map = $this->loadSeachCondition();
        $map['flag'] = 1;
        $total = $Model->where($map)->count();// 查询满足要求的总记录数
        $page = json_decode($this->pageParam($total));
        $data = $Model->where($map)->limit($page->firstRow, $page->listRows)->order($page->sort)->select();
        for ($i = 0; $i < count($data); $i++) {
            $user = $UserModel->where("id=" . $data[$i]['user_id'])->field("username")->find();
            if ($user) {
                $data[$i]['username'] = $user['username'];
            } else {
                $data[$i]['username'] = "无";
            }
            $SchoolArea = $SchoolAreaModel->where("id=" . $data[$i]['school_area_id'])->field("area_name")->find();
            if ($SchoolArea) {
                $data[$i]['area_name'] = $SchoolArea['area_name'];
            } else {
                $data[$i]['area_name'] = "无";
            }
            $ShopProductCount = $ShopProductModel->where("flag=1 and shop_id=" . $data[$i]['id'])->count();
            $data[$i]['num'] = $ShopProductCount;

        }
        $vo = $this->toJosnForGird($data, $page);
        echo json_encode($vo);
    }

    /**
     * 组装条件
     * @return multitype:multitype:string
     */
    public function loadSeachCondition()
    {
        $map = array();
        /*
        if(!empty($_GET['field'])){
            $map['field'] = array("like","%".$_GET['field']."%");
        }
        */
        return $map;
    }

    // 新增
    public function add()
    {
        $ProvinceModel = M('ZhProvince');
        $CityModel = M('ZhCity');
        $AreaModel = M('ZhArea');
        $School = M("School");
        $schoolList = $School->field("id,name")->where(array('flag' => 1))->select();
        $province = $ProvinceModel->select();
        $city = $CityModel->where("province_id=" . $province[0][id])->select();
        $area = $AreaModel->where("city_id=" . $city[0][id])->select();
        $this->assign("schoolList", $schoolList);
        $this->assign("province", $province);
        $this->assign("city", $city);
        $this->assign("area", $area);
        $this->display();
    }

    /**
     * 获取城市
     */
    public function getCity()
    {
        $CityModel = M('ZhCity');
        $AreaModel = M('ZhArea');
        $province = $_REQUEST["province"];
        $city = $CityModel->where("province_id=" . $province)->select();
        $area = $AreaModel->where("city_id=" . $city[0][id])->select();
        $vo[0] = $city;
        $vo[1] = $area;
        $this->ajaxReturn($vo, 1, 0);
    }

    /**
     * 获取区
     */
    public function getArea()
    {
        $AreaModel = M('ZhArea');
        $city = $_REQUEST["city"];
        $vo = $AreaModel->where("city_id=" . $city)->select();
        $this->ajaxReturn($vo, 1, 0);
    }

    public function insert()
    {
        $Model = M($this->getActionName());
        unset ($_POST [$Model->getPk()]);
        if (false === $Model->create()) {
            $this->error($Model->getError());
        }
        $user = M("User")->where(array("username" => $_POST['username']))->field("id")->find();
        if (empty($user)) {
            $this->ajaxReturn(0, L('ADMIN_ADD_ERROR'), 0);
        }
        $data = $Model->create();
        $map['flag'] = 1;
        $max_seq = $Model->where($map)->order('seq desc')->getField('seq');
        $data['seq'] = $max_seq + 1;
        $data['user_id'] = $user['id'];
        //保存当前数据对象
        if ($result = $Model->add($data)) { //保存成功
            M("User")->where(array('id' => $user['id']))->setField('is_shop', 1);
            $this->ajaxReturn(1, L('ADMIN_ADD_SUCCESS'), 1);
        } else {
            $this->ajaxReturn(0, L('ADMIN_ADD_ERROR'), 0);
        }
    }


    //修改店铺
    public function edit()
    {
        $Model = M($this->getActionName());
        $UserModel = M('User');
        $SchoolAreaModel = M("SchoolAreas");
        $School = M("School");
        $map['id'] = $_REQUEST['id'];
        $vo = $Model->where($map)->find();

        //学校区域信息
        $school_area_info = $SchoolAreaModel->where("id=" . $vo['school_area_id'])->field("id,school_id")->find();
        $school_area = $SchoolAreaModel->where("school_id=" . $school_area_info['school_id'])->field("id,area_name")->select();

        $school_floor = M("SchoolFloor")->where("school_id=" . $school_area_info['school_id'] . " and flag=1")->field("floor_num")->select();

        $schoolList = $School->field("id,name")->where(array('flag' => 1))->select();
        //商家账号信息
        $user = $UserModel->where("id=" . $vo['user_id'])->field("username")->find();
        $vo['username'] = $user['username'];
        $this->assign('school_area', $school_area);
        $this->assign('school_floor', $school_floor);
        $this->assign('username', $user['username']);
        $this->assign('schoolList', $schoolList);
        $this->assign('vo', $vo);
        $this->display();
    }

    /**
     * 更新店铺
     */
    public function update()
    {
        $Model = D($this->getActionName());

        if (false === $Model->create()) {
            $this->error($Model->getError());
        }

        $param = $_REQUEST;
        $old_username = $param['old_username'];
        unset($param['old_username']);
        $user = M("User")->where(array("username" => $param['username']))->field("id")->find();
        if (empty($user)) {
            $this->ajaxReturn(0, L('ADMIN_EDIT_ERROR'), 0);
        }
        $old_user = M("User")->where(array("username" => $old_username))->field("id")->find();
        $_REQUEST['user_id'] = $user['id'];
        // 更新数据
        if (false !== $Model->save($_REQUEST)) {
            //判断号码是否修改
            if ($old_username != $param['username']) {
                M("User")->where(array('id' => $user['id']))->setField('is_shop', 1);
                if (!empty($old_user)) {
                    M("User")->where(array('id' => $old_user['id']))->setField('is_shop', 0);
                }
            }
            $this->ajaxReturn(1, L('ADMIN_EDIT_SUCCESS'), 1);
        } else {
            $this->ajaxReturn(0, L('ADMIN_EDIT_ERROR'), 0);
        }
    }

    /**
     * 逻辑删除店铺
     */
    public function logicDel()
    {
        $Model = M($this->getActionName());
        $ShopProductModel = M("ShopProduct");
        if (!empty($Model)) {
            $pk = $Model->getPk();
            $id = $_REQUEST[$pk];
            if (isset($id)) {
                $map['flag'] = 1;
                $map['shop_id'] = $id;
                $count = $ShopProductModel->where($map)->count();
                if ($count != 0) {
                    $this->ajaxReturn(0, "删除失败！该店铺有上传商品！", 0);
                }
                $condition = array($pk => array('in', explode(',', $id)));
                if (false !== $Model->where($condition)->setField('flag', 2)) {
                    $this->ajaxReturn(1, L('ADMIN_DELETE_SUCCESS'), 1);
                } else {
                    $this->ajaxReturn(0, L('ADMIN_DELETE_ERROR'), 0);
                }
            } else {
                $this->ajaxReturn(0, L('ADMIN_DELETE_ERROR'), 0);
            }
        }
    }

    /**
     * @authority 修改排序
     */
    public function editSeq()
    {
        $Model = D($this->getActionName());
        $map['flag'] = 1;
        $data = $Model->where($map)->order('seq asc')->field('id,name as name')->select();
        $newData = array();
        $newData[0]['id'] = -1;
        $newData[0]['pId'] = 0;
        $newData[0]['name'] = "店铺";
        $newData[0]['isParent'] = true;
        $newData[0]['open'] = true;
        for ($i = 0; $i < count($data); $i++) {
            $newData[$i + 1]['id'] = $data[$i]['id'];
            $newData[$i + 1]['pId'] = $newData[0]['id'];
            $newData[$i + 1]['name'] = $data[$i]['name'];
        }
        $this->assign("zNodes", json_encode($newData));
        $this->display();
    }

    /**
     * @authority 更新排序
     */
    public function updateSeq()
    {
        $Model = D($this->getActionName());
        //批量更新
        $seq = $_REQUEST['seq'];
        $sql = 'SET seq = CASE id ';
        $ids = "";
        foreach ($seq as $key => $val) {
            $ids .= $val . ",";
            $key++;
            $sql .= ' WHEN ' . $val . ' THEN ' . $key . " ";
        }
        $sql .= ' END ';
        $ids = substr($ids, 0, strlen($ids) - 1);
        $sql .= 'where id in (' . $ids . ')';
        $where['id'] = array('in', $ids);
        $data['_query'] = $sql;
        $result = $Model->execute('update __TABLE__ ' . $sql);
        if ($result !== false) {
            $this->deleteSession();
            $this->ajaxReturn(1, L('ADMIN_SEQ_SUCCESS'), 1);
        } else {
            $this->ajaxReturn(0, L('ADMIN_SEQ_ERROR'), 0);
        }
    }

    /**
     * @authority 修改店铺商品排序
     */
    public function editProductSeq()
    {
        $Model = D("ShopProduct");
        $map['a.flag'] = 1;
        $map['a.shop_id'] = $_REQUEST['id'];
        $data = $Model->alias('a')->join('left join __PRODUCT__ as b on b.id = a.product_id')->where($map)->order('a.seq asc')->field('a.id,a.product_id,a.num,b.name as name,b.is_attr')->select();

        $newData = array();
        $newData[0]['id'] = -1;
        $newData[0]['pId'] = 0;
        $newData[0]['name'] = "店铺商品";
        $newData[0]['isParent'] = true;
        $newData[0]['open'] = true;
        for ($i = 0; $i < count($data); $i++) {
            $newData[$i + 1]['id'] = $data[$i]['id'];
            $newData[$i + 1]['pId'] = $newData[0]['id'];
            $newData[$i + 1]['name'] = $data[$i]['name']." 库存：".$data[$i]['num'];
            if ($data[$i]['is_attr'] == 1) {
                $attribute_values = M("ShopAttributeValues")->where(array('shop_id' => $_REQUEST['id'], 'product_id' => $data[$i]['product_id'], 'flag' => 1))->sum("num");
                $newData[$i + 1]['num'] = $attribute_values;
                /*
                $attribute_values = M("ShopAttributeValues")->where(array('shop_id' => $_REQUEST['id'], 'product_id' => $data[$i]['product_id'], 'flag' => 1))->select();
                if (!empty($attribute_values)) {
                    $newData[$i + 1]['attribute_values'] = $attribute_values;
                } else {
                    $newData[$i + 1]['attribute_values'] = array();
                }
                */
            }
        }
        $this->assign("zNodes", json_encode($newData));
        $this->display();
    }

    /**
     * @authority 更新店铺商品排序
     */
    public function updateProductSeq()
    {
        $Model = D("ShopProduct");
        //批量更新
        $seq = $_REQUEST['seq'];
        $sql = 'SET seq = CASE id ';
        $ids = "";
        foreach ($seq as $key => $val) {
            $ids .= $val . ",";
            $key++;
            $sql .= ' WHEN ' . $val . ' THEN ' . $key . " ";
        }
        $sql .= ' END ';
        $ids = substr($ids, 0, strlen($ids) - 1);
        $sql .= 'where id in (' . $ids . ')';
        $where['id'] = array('in', $ids);
        $data['_query'] = $sql;
        $result = $Model->execute('update __TABLE__ ' . $sql);
        if ($result !== false) {
            $this->deleteSession();
            $this->ajaxReturn(1, L('ADMIN_SEQ_SUCCESS'), 1);
        } else {
            $this->ajaxReturn(0, L('ADMIN_SEQ_ERROR'), 0);
        }
    }

    //检查学校是否存在
    public function checkSchool()
    {
        $param = $_REQUEST['param'];
        $user = M("School");
        $where['name'] = $param;
        $where['flag'] = 1;
        $count = $user->where($where)->count();
        if ($count) {
            echo 'y';
        } else {
            echo '此学校不存在';
        }
    }

    //检查用户是否存在
    public function checkUser()
    {
        $param = $_REQUEST['param'];
        $user = M("User");
        $where['username'] = $param;
        $where['flag'] = 1;
        if ($_REQUEST['old_username'] == $param) {
            echo 'y';
            exit;
        }
        $userInfo = $user->where($where)->find();
        if ($userInfo) {
            $shop = M("Shop")->where(array("user_id" => $userInfo['id']))->find();
            if ($shop) {
                echo '此账号已开店';
            } else {
                echo 'y';
            }
        } else {
            echo '此用户不存在';
        }
    }


    //检查店铺名是否可注册
    public function checkName()
    {
        $param = $_REQUEST['param'];
        $user = M($this->getActionName());
        $where['name'] = $param;
        $where['flag'] = 1;
        $count = $user->where($where)->count();
        if ($count) {
            echo '学校名已被注册！';
        } else {
            echo 'y';
        }
    }

    //检查店铺名是否可注册，排除当前
    public function exCheckName()
    {
        $param = $_REQUEST['param'];
        $user = M($this->getActionName());
        if (!empty($_REQUEST['id'])) {
            $map['id'] = $_REQUEST['id'];
            $result = $user->where($map)->find();
            if ($param == $result['name']) {
                echo 'y';
                return;
            } else {
                $where['name'] = $param;
                $where['flag'] = 1;
            }
        } else {
            $where['name'] = $param;
            $where['flag'] = 1;
        }
        $count = $user->where($where)->count();
        if ($count) {
            echo '学校名已被注册！';
        } else {
            echo 'y';
        }
    }

    /**
     * 更改状态
     */
    public function updateStatus()
    {
        //删除指定记录
        $Model = M($this->getActionName());
        $pk = $Model->getPk();
        $id = $_REQUEST[$pk];
        if (isset($id)) {
            $condition = array($pk => array('in', explode(',', $id)));
            if (false !== $Model->where($condition)->setField($_REQUEST['field'], $_REQUEST['status'])) {
                $this->deleteSession();
                $this->ajaxReturn(1, L('ADMIN_EDIT_SUCCESS'), 1);
            } else {
                $this->ajaxReturn(0, L('ADMIN_EDIT_ERROR'), 0);
            }
        }
    }


    private function deleteSession()
    {
        S('S_SYSTEM_SCHOOL', null);
    }


    public function schoolAreaList()
    {
        $SareasModel = M('SchoolAreas');
        $SchoolModel = M('School');
        $school_name = $_REQUEST['school_name'];
        $School = $SchoolModel->where(array('name' => $school_name))->field("id,name")->find();
        $map['school_id'] = $School['id'];
        $map['flag'] = 1;
        $data = $SareasModel->where($map)->select();
        $this->ajaxReturn($data, 1, 0);
    }

    public function schoolFloorList()
    {
        $SchoolFloorModel = M('SchoolFloor');
        $SchoolModel = M('School');
        $school_name = $_REQUEST['school_name'];
        $School = $SchoolModel->where(array('name' => $school_name))->field("id,name")->find();
        $map['school_id'] = $School['id'];
        $map['flag'] = 1;
        $data = $SchoolFloorModel->where($map)->select();
        $this->ajaxReturn($data, 1, 0);
    }

    //图片传图
    public function uploadImg()
    {
        $folderName = $_POST['folderName'];//格式  首字母大写
        $prefix = $_POST['prefix'];//格式  m_,s_
        $maxWidth = $_POST['maxWidth'];//格式500,200
        $maxHeight = $_POST['maxHeight'];//格式500,200
        import('ORG.Net.UploadFile');
        $targetFolder_b = './Uploads/' . $folderName . '/' . date('Y-m-d') . '/';
        if (!is_dir($targetFolder_b)) {
            @mkdir($targetFolder_b, 0777);
        }
        $upload = new UploadFile();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath = $targetFolder_b;// 设置附件上传目录
        if ($maxWidth != 0 && $maxHeight != 0) {
            $upload->thumb = true;
            $upload->thumbPrefix = $prefix;  //缩略图前缀
            $upload->thumbMaxWidth = $maxWidth;
            $upload->thumbMaxHeight = $maxHeight;
            $upload->thumbType = 0;//按设置大小截取 0 按原图等比例缩略
            $upload->thumbRemoveOrigin = true; //上传后移除原图
        }
        if (!$upload->upload()) {// 上传错误提示错误信息
            $this->ajaxReturn(0, $upload->getErrorMsg(), 0);
        } else {// 上传成功
            $info = $upload->getUploadFileInfo();
            //补上缩略图的前缀
            if ($maxWidth != 0 && $maxHeight != 0) {
                $bak['url'] = ltrim($info[0]['savepath'], ".") . $prefix . $info[0]['savename'];
            } else {
                $bak['url'] = ltrim($info[0]['savepath'], ".") . $info[0]['savename'];
            }
            $bak['localname'] = $info[0]['name'];
            $this->ajaxReturn($bak, '上传成功！', 1);
        }
    }

    //店铺添加产品
    public function addProduct()
    {
        $Model = M($this->getActionName());
        $map[$Model->getPk()] = $_REQUEST['id'];
        $vo = $Model->where($map)->find();
        $TypeModel = M("Type");
        $condition['reserve'] = 1;
        $condition['flag'] = 1;
        $types = $TypeModel->where($condition)->field("id,type_name as name")->order("seq asc")->select();
        $this->assign("types", $types);
        $this->assign("vo", $vo);
        $this->display();
    }

    //已添加产品列表
    public function getShopProductData()
    {
        $Model = M("ShopProduct");
        $map = $this->loadProductSeachCondition();
        $map['a.shop_id'] = $_REQUEST['id'];
        $map['a.flag'] = 1;

        $where = $this->loadSeachCondition();
        $total = $Model->where($where)->count();// 查询满足要求的总记录数
        $page = json_decode($this->pageParam($total));
        if (!empty($page->sort)) {
            if ($page->sort == "is_attr desc" || $page->sort == "is_attr asc") {
                $sort = 'b.' . $page->sort;
            } else if ($page->sort == "name desc" || $page->sort == "name asc") {
                $sort = 'b.' . $page->sort;
            } else if ($page->sort == "seq desc" || $page->sort == "seq asc") {
                $sort = 'a.' . $page->sort;
            } else if ($page->sort == "num desc" || $page->sort == "num asc") {
                $sort = 'a.' . $page->sort;
            } else if ($page->sort == "id desc" || $page->sort == "id asc") {
                $sort = 'a.' . $page->sort;
            }
        } else {
            $sort = 'a.' . $page->sort;
        }
        $data = $Model->alias('a')->join('left join __PRODUCT__ as b on b.id = a.product_id')->field('a.id,a.product_id,b.name,a.num,a.shop_price,b.is_attr')->where($map)->limit($page->firstRow, $page->listRows)->order($sort)->select();
        if ($data) {
            foreach ($data as $p_key => $p_v) {
                if ($p_v['is_attr'] == 1) {
                    $attribute_values = M("ShopAttributeValues")->where(array('shop_id' => $_REQUEST['id'], 'product_id' => $p_v['product_id'], 'flag' => 1))->select();
                    if (!empty($attribute_values)) {
                        $data[$p_key]['attribute_values'] = $attribute_values;
                    } else {
                        $data[$p_key]['attribute_values'] = array();
                    }
                } else {
                    $data[$p_key]['attribute_values'] = array();
                }

            }
        }

        $vo = $this->toJosnForGird($data, $page);
        echo json_encode($vo);
    }

    //产品列表
    public function getProductAllData()
    {
        $Model = M("Product");
        $map = $this->loadProductSeachCondition();
        $map['a.flag'] = 1;
        $where = $this->loadSeachCondition();
        $where['flag'] = 1;
        $total = $Model->where($where)->count();// 查询满足要求的总记录数
        $page = json_decode($this->pageParam($total));
        if (!empty($page->sort)) {
            $sort = 'a.' . $page->sort;
        } else {
            $sort = $page->sort;
        }
        $data = $Model->alias('a')->join('left join __TYPE__ as b on b.id = a.type_id')->field('a.id,a.name,a.spec,a.desc,a.price,a.is_on,a.seq,a.create_time,b.type_name')->where($map)->limit($page->firstRow, $page->listRows)->order($sort)->select();
        $vo = $this->toJosnForGird($data, $page);
        echo json_encode($vo);
    }

    /**
     * 组装条件
     * @return multitype:multitype:string
     */
    public function loadProductSeachCondition()
    {
        $map = array();
        if (!empty($_GET['name'])) {
            $map['name'] = array("like", "%" . $_GET['name'] . "%");
        }
        if (!empty($_GET['type_id'])) {
            $map['type_id'] = $_GET['type_id'];
        }
        return $map;
    }

    /**
     * 添加至列表
     */
    public function addToList()
    {
        $ShopProductModel = M('ShopProduct');
        $ShopTypeModel = M('ShopType');
        $ShopSubtypeModel = M('ShopSubtype');
        $AttributeValues = M('AttributeValues');
        $data['product_id'] = $_REQUEST["product_id"];
        $productInfo = M("Product")->field("type_id,subtype_code,type_id,is_attr")->where(array("id" => $data['product_id']))->find();
        $data['shop_id'] = $_REQUEST["shop_id"];
        $data['subtype_code'] = $productInfo["subtype_code"];
        $data['type_id'] = $productInfo["type_id"];
        $data['flag'] = 1;

        $type_data['shop_id'] = $_REQUEST["shop_id"];
        $type_data['type_id'] = $productInfo["type_id"];
        $type_data['flag'] = 1;

        $subtype_data['shop_id'] = $_REQUEST["shop_id"];
        $subtype_data['subtype_code'] = $productInfo["subtype_code"];
        $subtype_data['flag'] = 1;

        //如果有规格   超市商品添加商品规格
        if ($productInfo['is_attr'] == 1) {
            $attr_data = $AttributeValues->where(array('product_id' => $_REQUEST['product_id'], 'flag' => 1))->select();
            if ($attr_data) {
                foreach ($attr_data as $s_attr_key => $s_attr_v) {
                    $attr_data[$s_attr_key]['attr_value_id'] = $attr_data[$s_attr_key]['id'];
                    $attr_data[$s_attr_key]['flag'] = 1;
                    $attr_data[$s_attr_key]['num'] = 0;
                    $attr_data[$s_attr_key]['shop_id'] = $_REQUEST["shop_id"];
                    unset($attr_data[$s_attr_key]['id']);
                }
            }
        }
        $count = $ShopProductModel->where($data)->count();
        $type_count = $ShopTypeModel->where($type_data)->count();
        $subtype_count = $ShopSubtypeModel->where($subtype_data)->count();
        if ($count == 0) {
            $ShopProductModel->add($data);
            M("ShopAttributeValues")->addAll($attr_data);
            if ($type_count == 0) {
                $ShopTypeModel->add($type_data);
            }
            if ($subtype_count == 0) {
                $ShopSubtypeModel->add($subtype_data);
            }
            $this->ajaxReturn(1, L('ADMIN_ADD_SUCCESS'), 0);
        } else {
            $this->ajaxReturn(0, "重复添加！", 1);
        }
    }

    //删除店铺商品
    function del_shop_product()
    {
        $Model = M("ShopProduct");
        if (!empty($Model)) {
            $pk = $Model->getPk();
            $id = $_REQUEST[$pk];
            if (isset($id)) {
                $condition = array($pk => array('in', $id));
                if (false !== $Model->where($condition)->setField('flag', 2)) {
                    //$this->deleteSession();
                    $id_arr = $id;
                    $ShopTypeModel = M('ShopType');
                    $ShopSubtypeModel = M('ShopSubtype');
                    $product_id_arr = array();
                    $shop_id = 0;
                    foreach ($id_arr as $s_p_key => $s_p_id) {
                        $shop_product = $Model->where(array('id' => $s_p_id))->field('product_id,shop_id,type_id,subtype_code')->find();
                        $product_id_arr[] = $shop_product['product_id'];
                        $shop_id = $shop_product['shop_id'];
                        $shop_sub_p_count = $Model->where(array("shop_id" => $shop_product['shop_id'], 'subtype_code' => $shop_product['subtype_code'], 'flag' => 1))->count();
                        if ($shop_sub_p_count == 0) {
                            $ShopSubtypeModel->where(array(array("shop_id" => $shop_product['shop_id'], 'subtype_code' => $shop_product['subtype_code'])))->setField('flag', 2);
                        }
                        $shop_type_p_count = $Model->where(array("shop_id" => $shop_product['shop_id'], 'type_id' => $shop_product['type_id'], 'flag' => 1))->count();
                        if ($shop_type_p_count == 0) {
                            $ShopTypeModel->where(array(array("shop_id" => $shop_product['shop_id'], 'type_id' => $shop_product['type_id'])))->setField('flag', 2);
                        }
                    }
                    //删除超市该产品的规格
                    if (!empty($product_id_arr) && $shop_id != 0) {
                        M("ShopAttributeValues")->where(array('shop_id' => $shop_id, 'product_id' => array('in', $product_id_arr)))->delete();
                    }
                    //删除超市会场中的该产品
                    if (!empty($product_id_arr) && $shop_id != 0) {
                        M("MeetingplaceProduct")->where(array('shop_id' => $shop_id, 'product_id' => array('in', $product_id_arr)))->delete();
                    }
                    $this->ajaxReturn(1, L('ADMIN_DELETE_SUCCESS'), 1);
                } else {
                    $this->ajaxReturn(0, L('ADMIN_DELETE_ERROR'), 0);
                }
            } else {
                $this->ajaxReturn(0, L('ADMIN_DELETE_ERROR'), 0);
            }
        }

    }

    //更新超市商品信息
    function updateShopProduct()
    {
        $Model = M("ShopProduct");
        if (!empty($Model)) {
            $id = $_REQUEST['id'];
            $num = $_REQUEST['num'];
            $shop_price = $_REQUEST['shop_price'];
            $set = array();
            if ($num) {
                $set['num'] = $num;
            }
            if ($shop_price) {
                $set['shop_price'] = $shop_price;
            }
            if (isset($id)) {
                $condition = array(id => $id);
                if (false !== $Model->where($condition)->save($set)) {
                    $this->ajaxReturn(1, L('INDEX_UPDATE_SUCCESS'), 1);
                } else {
                    $this->ajaxReturn(0, L('INDEX_UPDATE_ERROR'), 0);
                }
            } else {
                $this->ajaxReturn(0, L('INDEX_UPDATE_ERROR'), 0);
            }
        }
    }

    //更新超市商品库存
    function updatenum()
    {
        $Model = M("ShopProduct");
        if (!empty($Model)) {
            $id = $_REQUEST['id'];
            $num = $_REQUEST['num'];
            if (isset($id)) {
                $condition = array(id => $id);
                if (false !== $Model->where($condition)->setField('num', $num)) {
                    $this->ajaxReturn(1, L('INDEX_UPDATE_SUCCESS'), 1);
                } else {
                    $this->ajaxReturn(0, L('INDEX_UPDATE_ERROR'), 0);
                }
            } else {
                $this->ajaxReturn(0, L('INDEX_UPDATE_ERROR'), 0);
            }
        }
    }

    //更新超市商品库存
    function updateattrnum()
    {
        $Model = M("ShopProduct");
        if (!empty($Model)) {
            $id = $_REQUEST['id'];
            $attr_num = $_REQUEST['attr_num'];
            $shop_price = $_REQUEST['shop_price'];
            if (isset($id)) {
                if ($attr_num) {
                    foreach ($attr_num as $attr_key => $attr_v) {
                        $attr_arr = explode(":", $attr_v);
                        M("ShopAttributeValues")->where(array("id" => $attr_arr[0]))->setField('num', $attr_arr[1]);
                    }
                }
                if ($shop_price && $shop_price > 0) {
                    $condition = array(id => $id);
                    $Model->where($condition)->setField('shop_price', $shop_price);
                }
                $this->ajaxReturn(1, L('INDEX_UPDATE_SUCCESS'), 1);
            } else {
                $this->ajaxReturn(0, L('INDEX_UPDATE_ERROR'), 0);
            }
        }
    }

    //店铺配货
    public function distribution()
    {
        $Model = M($this->getActionName());
        $map[$Model->getPk()] = $_REQUEST['id'];
        $vo = $Model->where($map)->find();
        $TypeModel = M("Type");
        $condition['reserve'] = 1;
        $condition['flag'] = 1;
        $types = $TypeModel->where($condition)->field("id,type_name as name")->order("seq asc")->select();
        $this->assign("types", $types);
        $this->assign("vo", $vo);
        $this->display();
    }

    //已配货产品列表
    public function getShopDistributionData()
    {
        $Model = M("ShopDistribution");
        $map = $this->loadProductSeachCondition();
        $map['a.shop_id'] = $_REQUEST['id'];
        $map['a.flag'] = 1;

        $where = $this->loadSeachCondition();
        $total = $Model->where($where)->count();// 查询满足要求的总记录数
        $page = json_decode($this->pageParam($total));
        if (!empty($page->sort)) {
            if ($page->sort == "is_attr desc" || $page->sort == "is_attr asc") {
                $sort = 'b.' . $page->sort;
            } else if ($page->sort == "name desc" || $page->sort == "name asc") {
                $sort = 'b.' . $page->sort;
            } else if ($page->sort == "num desc" || $page->sort == "num asc") {
                $sort = 'a.' . $page->sort;
            } else if ($page->sort == "id desc" || $page->sort == "id asc") {
                $sort = 'a.' . $page->sort;
            } else if ($page->sort == "status desc" || $page->sort == "status asc") {
                $sort = 'a.' . $page->sort;
            }
        } else {
            $sort = 'a.' . $page->sort;
        }
        $map_str = "";
        foreach ($map as $map_k => $map_v) {
            if (is_array($map_v)) {
                if ($map_str == "") {
                    $map_str .= " " . $map_k . " " . $map_v[0] . " '" . $map_v[1] . "' ";
                } else {
                    $map_str .= "and " . $map_k . " " . $map_v[0] . " '" . $map_v[1] . "' ";
                }
            } else {
                if ($map_str == "") {
                    $map_str .= " " . $map_k . "=" . $map_v . " ";
                } else {
                    $map_str .= "and " . $map_k . "=" . $map_v . " ";
                }
            }

        }
        if (isset($_REQUEST['ctime'])) {
            $map_str .= " and TO_DAYS(FROM_UNIXTIME(ctime)) = TO_DAYS('" . $_REQUEST['ctime'] . "')";
        } else {
            $map_str .= " and TO_DAYS(FROM_UNIXTIME(ctime)) = TO_DAYS(NOW())";
        }
        $data = $Model->alias('a')->join('left join __PRODUCT__ as b on b.id = a.product_id')->field('a.id,a.product_id,b.name,a.num,b.is_attr,a.status,a.disposition')->where($map_str)->limit($page->firstRow, $page->listRows)->order($sort)->select();
        if ($data) {
            foreach ($data as $p_key => $p_v) {
                if ($p_v['is_attr'] == 1) {
                    $attribute_values = M("AttributeValues")->where(array('shop_id' => $_REQUEST['id'], 'product_id' => $p_v['product_id'], 'flag' => 1))->getField("id,product_id,attr_id,attr_name,attr_value_name, 0 as num");
                    $att_arr = json_decode($p_v['disposition'], true);
                    foreach ($att_arr as $attr_key => $attr_v) {
                        $attr_v_arr = explode(":", $attr_v);
                        if (isset($attr_v_arr[0]) && isset($attr_v_arr[1])) {
                            $attribute_values[$attr_v_arr[0]]['num'] = $attr_v_arr[1];
                        }
                    }
                    if (!empty($attribute_values)) {
                        $data[$p_key]['attribute_values'] = $attribute_values;
                    } else {
                        $data[$p_key]['attribute_values'] = array();
                    }
                } else {
                    $data[$p_key]['attribute_values'] = array();
                }

            }
        }
        $vo = $this->toJosnForGird($data, $page);
        echo json_encode($vo);
    }

    /**
     * 添加至配货列表
     */
    public function addToDistributionList()
    {
        $ShopDistributionModel = M('ShopDistribution');
        $productInfo = M("Product")->field("type_id,subtype_code,type_id,is_attr")->where(array("id" => $_REQUEST['product_id']))->find();
        $data['product_id'] = $_REQUEST["product_id"];
        $data['shop_id'] = $_REQUEST["shop_id"];
        $data['flag'] = 1;
        $map_str = "";
        foreach ($data as $map_k => $map_v) {
            if (is_array($map_v)) {
                if ($map_str == "") {
                    $map_str .= " " . $map_k . " " . $map_v[0] . " '" . $map_v[1] . "' ";
                } else {
                    $map_str .= "and " . $map_k . " " . $map_v[0] . " '" . $map_v[1] . "' ";
                }
            } else {
                if ($map_str == "") {
                    $map_str .= " " . $map_k . "=" . $map_v . " ";
                } else {
                    $map_str .= "and " . $map_k . "=" . $map_v . " ";
                }
            }
        }
        $map_str .= " and TO_DAYS(FROM_UNIXTIME(ctime)) = TO_DAYS(NOW())";
        $count = $ShopDistributionModel->where($map_str)->count();
        if ($count == 0) {
            $data['subtype_code'] = $productInfo["subtype_code"];
            $data['type_id'] = $productInfo["type_id"];
            $data['is_attr'] = $productInfo["is_attr"];
            $data['status'] = 0;
            $data['disposition'] = "";
            $data['realname'] = $_SESSION['ADMIN_AUTH_KEY_REALNAME'];
            $data['ctime'] = time();
            $ShopDistributionModel->add($data);
            $this->ajaxReturn(1, L('ADMIN_ADD_SUCCESS'), 0);
        } else {
            $this->ajaxReturn(0, "今日配货单此商品已存在！", 1);
        }
    }

    //更新超市商品库存
    function updateDistributionNum()
    {
        $Model = M("ShopDistribution");
        if ($_REQUEST['distribution_1']) {
            foreach ($_REQUEST['distribution_1'] as $dis_v) {
                $dis_arr = explode("|", $dis_v);
                if (isset($dis_arr[1]) && isset($dis_arr[0]) && $dis_arr[1] > 0) {
                    $condition = array('id' => $dis_arr[0]);
                    $Model->where($condition)->setField('num', $dis_arr[1]);
                }
            }
        }
        if ($_REQUEST['distribution_2']) {
            foreach ($_REQUEST['distribution_2'] as $dis_v) {
                $dis_arr = explode("|", $dis_v);
                if (isset($dis_arr[1]) && isset($dis_arr[0])) {
                    $dis_arr = explode("|", $dis_v);
                    $attr_arr = explode(",", $dis_arr[1]);
                    $disposition = json_encode($attr_arr);
                    $condition = array('id' => $dis_arr[0]);
                    $Model->where($condition)->setField('disposition', $disposition);
                }
            }
        }
        $this->ajaxReturn(1, L('INDEX_UPDATE_SUCCESS'), 1);

    }

    //删除店铺删除为确认配货的记录
    function delShopDistribution()
    {
        $Model = M("ShopDistribution");
        if (!empty($Model)) {
            $pk = $Model->getPk();
            $id = $_REQUEST['id'];
            if (isset($id)) {
                $condition = array($pk => array('in', $id), "status" => 0);
                if (false !== $Model->where($condition)->setField('flag', 2)) {
                    $this->ajaxReturn(1, L('ADMIN_DELETE_SUCCESS'), 1);
                } else {
                    $this->ajaxReturn(0, L('ADMIN_DELETE_ERROR'), 0);
                }
            } else {
                $this->ajaxReturn(0, L('ADMIN_DELETE_ERROR'), 0);
            }
        }

    }

    //配货单
    public function distributionView()
    {
        $Model = M("ShopDistribution");
        $map = $this->loadProductSeachCondition();
        $map['a.shop_id'] = $_REQUEST['id'];
        $map['a.flag'] = 1;
        $shop = M("Shop")->where("id=" . $_REQUEST['id'])->find();
        $this->assign("shop", $shop);
        $this->assign("realname", $_SESSION['ADMIN_AUTH_KEY_REALNAME']);
        $where = $this->loadSeachCondition();
        $total = $Model->where($where)->count();// 查询满足要求的总记录数
        $page = json_decode($this->pageParam($total));
        if (!empty($page->sort)) {
            if ($page->sort == "is_attr desc" || $page->sort == "is_attr asc") {
                $sort = 'b.' . $page->sort;
            } else if ($page->sort == "name desc" || $page->sort == "name asc") {
                $sort = 'b.' . $page->sort;
            } else if ($page->sort == "num desc" || $page->sort == "num asc") {
                $sort = 'a.' . $page->sort;
            } else if ($page->sort == "id desc" || $page->sort == "id asc") {
                $sort = 'a.' . $page->sort;
            }
        } else {
            $sort = 'a.' . $page->sort;
        }
        $map_str = "";
        foreach ($map as $map_k => $map_v) {
            if (is_array($map_v)) {
                if ($map_str == "") {
                    $map_str .= " " . $map_k . " " . $map_v[0] . " '" . $map_v[1] . "' ";
                } else {
                    $map_str .= "and " . $map_k . " " . $map_v[0] . " '" . $map_v[1] . "' ";
                }
            } else {
                if ($map_str == "") {
                    $map_str .= " " . $map_k . "=" . $map_v . " ";
                } else {
                    $map_str .= "and " . $map_k . "=" . $map_v . " ";
                }
            }

        }
        if (isset($_REQUEST['ctime'])) {
            $this->assign("ctime", $_REQUEST['ctime']);
            $map_str .= " and TO_DAYS(FROM_UNIXTIME(ctime)) = TO_DAYS('" . $_REQUEST['ctime'] . "')";
        } else {
            $this->assign("ctime", date("Y-m-d"));
            $map_str .= " and TO_DAYS(FROM_UNIXTIME(ctime)) = TO_DAYS(NOW())";
        }
        $data = $Model->alias('a')->join('left join __PRODUCT__ as b on b.id = a.product_id')->field('a.id,a.product_id,b.name,a.num,b.is_attr,b.price,a.status,a.disposition,a.realname')->where($map_str)->limit($page->firstRow, $page->listRows)->order($sort)->select();

        if ($data) {
            foreach ($data as $p_key => $p_v) {
                if ($p_v['is_attr'] == 1) {
                    $attribute_values = M("AttributeValues")->where(array('shop_id' => $_REQUEST['id'], 'product_id' => $p_v['product_id'], 'flag' => 1))->getField("id,product_id,attr_id,attr_name,attr_value_name, 0 as num");
                    $att_arr = json_decode($p_v['disposition'], true);
                    foreach ($att_arr as $attr_key => $attr_v) {
                        $attr_v_arr = explode(":", $attr_v);
                        if (isset($attr_v_arr[0]) && isset($attr_v_arr[1])) {
                            $attribute_values[$attr_v_arr[0]]['num'] = $attr_v_arr[1];
                        }
                    }
                    if (!empty($attribute_values)) {
                        $data[$p_key]['attribute_values'] = $attribute_values;
                    } else {
                        $data[$p_key]['attribute_values'] = array();
                    }
                } else {
                    $data[$p_key]['attribute_values'] = array();
                }

            }
        }
        $this->assign("vo", $data);
        $this->display();
    }

    //审核配货单
    public function auditDistributionView()
    {
        $Model = M("ShopDistribution");
        $map = $this->loadProductSeachCondition();
        $map['a.shop_id'] = $_REQUEST['id'];
        $map['a.flag'] = 1;
        $map['a.status'] = 0;
        $map_str = "";
        foreach ($map as $map_k => $map_v) {
            if (is_array($map_v)) {
                if ($map_str == "") {
                    $map_str .= " " . $map_k . " " . $map_v[0] . " '" . $map_v[1] . "' ";
                } else {
                    $map_str .= "and " . $map_k . " " . $map_v[0] . " '" . $map_v[1] . "' ";
                }
            } else {
                if ($map_str == "") {
                    $map_str .= " " . $map_k . "=" . $map_v . " ";
                } else {
                    $map_str .= "and " . $map_k . "=" . $map_v . " ";
                }
            }
        }
        if (isset($_REQUEST['ctime'])) {
            $this->assign("ctime", $_REQUEST['ctime']);
            $map_str .= " and TO_DAYS(FROM_UNIXTIME(ctime)) = TO_DAYS('" . $_REQUEST['ctime'] . "')";
        } else {
            $this->assign("ctime", date("Y-m-d"));
            $map_str .= " and TO_DAYS(FROM_UNIXTIME(ctime)) = TO_DAYS(NOW())";
        }

        $data = $Model->alias('a')->join('left join __PRODUCT__ as b on b.id = a.product_id')->field('a.id,a.product_id,b.name,a.type_id,a.subtype_code,a.num,b.is_attr,b.price,a.status,a.disposition,a.realname')->where($map_str)->select();
        if ($data) {
            $insert_shop_sql = "INSERT INTO hs_shop_product (shop_id, product_id, type_id, subtype_code, num, seq, flag) VALUES";
            $is_in_shop = 0;
            $update_shop_sql = "UPDATE hs_shop_product SET num = num + CASE product_id";
            $is_up_shop = 0;
            $product_id_arr = array();

            $update_shop_attr_sql = "UPDATE hs_shop_attribute_values SET num = num + CASE attr_value_id";
            $is_up_shop_attr = 0;
            $insert_shop_attr_sql = "INSERT INTO hs_shop_attribute_values (shop_id, product_id, attr_id, attr_name,attr_value_id,attr_value_name,is_default, num, create_time) VALUES";
            $is_in_shop_attr = 0;
            $attr_value_id_arr = array();
            $distribution_id_arr = array();
            foreach ($data as $p_key => $p_v) {
                $distribution_id_arr[] = $p_v['id'];
                $total = M("ShopProduct")->where("shop_id=" . $_REQUEST['id'] . " and product_id=" . $p_v['product_id'] . " and flag=1")->count();// 查询满足要求的总记录数
                if ($total > 0) {
                    $product_id_arr[] = $p_v['product_id'];
                    $update_shop_sql .= " WHEN " . $p_v['product_id'] . " THEN " . $p_v['num'] . " ";
                    $is_up_shop = 1;
                } else {
                    $insert_shop_sql .= " (" . $_REQUEST['id'] . "," . $p_v['product_id'] . "," . $p_v['type_id'] . "," . $p_v['subtype_code'] . "," . $p_v['num'] . ",0,1" . " ),";
                    $is_in_shop = 1;
                }
                if ($p_v['is_attr'] == 1) {
                    $attribute_values = M("AttributeValues")->where(array('shop_id' => $_REQUEST['id'], 'product_id' => $p_v['product_id'], 'flag' => 1))->getField("id,product_id,attr_id,attr_name,attr_value_name,is_default,create_time, 0 as num");
                    $att_arr = json_decode($p_v['disposition'], true);
                    foreach ($att_arr as $attr_key => $attr_v) {
                        $attr_v_arr = explode(":", $attr_v);
                        if (isset($attr_v_arr[0]) && isset($attr_v_arr[1])) {
                            $attribute_values[$attr_v_arr[0]]['num'] = $attr_v_arr[1];
                            $shop_id = $_REQUEST['id'];
                            $product_id = $p_v['product_id'];
                            $attr_id = $attribute_values[$attr_v_arr[0]]['attr_id'];
                            $attr_name = $attribute_values[$attr_v_arr[0]]['attr_name'];
                            $attr_value_id = $attribute_values[$attr_v_arr[0]]['id'];
                            $attr_value_name = $attribute_values[$attr_v_arr[0]]['attr_value_name'];
                            $is_default = $attribute_values[$attr_v_arr[0]]['is_default'];
                            $attr_num = $attr_v_arr[1];
                            $create_time = $attribute_values[$attr_v_arr[0]]['create_time'];
                            $shop_attr_v_totsl = M("ShopAttributeValues")->where("attr_value_id=" . $attr_v_arr[0] . " and shop_id=" . $_REQUEST['id'] . " and product_id=" . $p_v['product_id'] . " and flag=1")->count();// 查询满足要求的总记录数
                            if ($shop_attr_v_totsl > 0) {
                                $attr_value_id_arr[] = $attr_v_arr[0];
                                $update_shop_attr_sql .= " WHEN '" . $attr_v_arr[0] . "' THEN " . $attr_v_arr[1] . " ";
                                $is_up_shop_attr = 1;
                            } else {
                                $insert_shop_attr_sql .= " ('" . $shop_id . "','" . $product_id . "','" . $attr_id . "','" . $attr_name . "','" . $attr_value_id . "','" . $attr_value_name . "','" . $is_default . "','" . $attr_num . "','" . $create_time . "' ),";
                                $is_in_shop_attr = 1;
                            }
                        }
                    }
                    if (!empty($attribute_values)) {
                        $data[$p_key]['attribute_values'] = $attribute_values;
                    } else {
                        $data[$p_key]['attribute_values'] = array();
                    }
                }
            }
            $insert_shop_sql = substr($insert_shop_sql, 0, strlen($insert_shop_sql) - 1);
            $insert_shop_attr_sql = substr($insert_shop_attr_sql, 0, strlen($insert_shop_attr_sql) - 1);

            $update_shop_sql .= " END WHERE shop_id=" . $_REQUEST['id'] . " AND flag=1 AND product_id IN(" . implode(",", $product_id_arr) . ")";
            $update_shop_attr_sql .= " END WHERE shop_id=" . $_REQUEST['id'] . " AND flag=1 AND attr_value_id IN(" . implode(",", $attr_value_id_arr) . ")";
            if ($is_in_shop == 1) {
                M("")->execute($insert_shop_sql);
            }
            if ($is_up_shop == 1) {
                M("")->execute($update_shop_sql);
            }
            if ($is_in_shop_attr==1) {
                M("")->execute($insert_shop_attr_sql);
            }
            if ($is_up_shop_attr == 1) {
                M("")->execute($update_shop_attr_sql);
            }
        }
        if (!empty($distribution_id_arr)) {
            $condition = array('id' => array('in', $distribution_id_arr));
            $Model->where($condition)->setField(array("status" => 1));
        }
        $this->ajaxReturn(1, L('INDEX_UPDATE_SUCCESS'), 1);
    }

    //已配货产品列表
    public function shopDistributionList()
    {
        $shop_list = M("Shop")->where(array('flag' => 1))->select();
        $this->assign("shop_list", $shop_list);
        $this->display('shopDistributionList');
    }

    //已配货产品列表
    public function getAllDistributionData()
    {
        $Model = M("ShopDistribution");
        $map['a.flag'] = 1;
        if (isset($_REQUEST['shop_id']) && $_REQUEST['shop_id']) {
            $map['a.shop_id'] = $_REQUEST['shop_id'];
        }
        if (!empty($_GET['name'])) {
            $map['b.name'] = array("like", "%" . $_GET['name'] . "%");
        }
        if (isset($_REQUEST['status'])) {
            $map['a.status'] = $_REQUEST['status'];
        }
        $where = $this->loadSeachCondition();
        $total = $Model->where($where)->count();// 查询满足要求的总记录数
        $page = json_decode($this->pageParam($total));
        if (!empty($page->sort)) {
            if ($page->sort == "is_attr desc" || $page->sort == "is_attr asc") {
                $sort = 'b.' . $page->sort;
            } else if ($page->sort == "name desc" || $page->sort == "name asc") {
                $sort = 'b.' . $page->sort;
            } else if ($page->sort == "num desc" || $page->sort == "num asc") {
                $sort = 'a.' . $page->sort;
            } else if ($page->sort == "id desc" || $page->sort == "id asc") {
                $sort = 'a.' . $page->sort;
            } else if ($page->sort == "status desc" || $page->sort == "status asc") {
                $sort = 'a.' . $page->sort;
            }
        } else {
            $sort = 'a.' . $page->sort;
        }
        $map_str = "";
        foreach ($map as $map_k => $map_v) {
            if (is_array($map_v)) {
                if ($map_str == "") {
                    $map_str .= " " . $map_k . " " . $map_v[0] . " '" . $map_v[1] . "' ";
                } else {
                    $map_str .= "and " . $map_k . " " . $map_v[0] . " '" . $map_v[1] . "' ";
                }
            } else {
                if ($map_str == "") {
                    $map_str .= " " . $map_k . "=" . $map_v . " ";
                } else {
                    $map_str .= "and " . $map_k . "=" . $map_v . " ";
                }
            }

        }
        if (isset($_REQUEST['ctime']) && $_REQUEST['ctime']) {
            $map_str .= " and TO_DAYS(FROM_UNIXTIME(ctime)) = TO_DAYS('" . $_REQUEST['ctime'] . "')";
        }

        $data = $Model->alias('a')->join('left join __PRODUCT__ as b on b.id = a.product_id')->join('left join __SHOP__ as c on c.id = a.shop_id')->field('a.id,a.product_id,from_unixtime(a.ctime) as ctime,b.name,a.num,b.is_attr,a.status,a.disposition,a.shop_id,c.school_name,c.name as shop_name,c.floor_num')->where($map_str)->limit($page->firstRow, $page->listRows)->order($sort)->select();
        if ($data) {
            foreach ($data as $p_key => $p_v) {
                if ($p_v['is_attr'] == 1) {
                    $attribute_values = M("AttributeValues")->where(array('shop_id' => $_REQUEST['id'], 'product_id' => $p_v['product_id'], 'flag' => 1))->getField("id,product_id,attr_id,attr_name,attr_value_name, 0 as num");
                    $att_arr = json_decode($p_v['disposition'], true);
                    foreach ($att_arr as $attr_key => $attr_v) {
                        $attr_v_arr = explode(":", $attr_v);
                        if (isset($attr_v_arr[0]) && isset($attr_v_arr[1])) {
                            $attribute_values[$attr_v_arr[0]]['num'] = $attr_v_arr[1];
                        }
                    }
                    if (!empty($attribute_values)) {
                        $data[$p_key]['attribute_values'] = $attribute_values;
                    } else {
                        $data[$p_key]['attribute_values'] = array();
                    }
                } else {
                    $data[$p_key]['attribute_values'] = array();
                }
            }
        }
        $vo = $this->toJosnForGird($data, $page);
        echo json_encode($vo);
    }
}